//
//  XJCMusicCollectViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/16.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "XJCMusicCollectViewController.h"
#import "CoreDataManager.h"
#import "MusicModel.h"
#import "MusicTableViewCell.h"
#import "XJCMusciModel.h"
#import "MusicDetailViewController.h"

@interface XJCMusicCollectViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableVlew;

@property (strong, nonatomic) NSArray *dataArray;

@end

@implementation XJCMusicCollectViewController

- (void)viewWillAppear:(BOOL)animated{
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MusicModel"];
    
    //向数据管理器发送请求
    _dataArray = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:request error:nil];
    [self.tableVlew reloadData];
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _tableVlew = [[UITableView alloc] initWithFrame:CURRENTSCEEN style:UITableViewStylePlain];
    
    _tableVlew.delegate = self;
    _tableVlew.dataSource = self;
    
    [self.view addSubview:_tableVlew];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cell";
    
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (!cell) {
        cell = [[MusicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    MusicModel *model = [_dataArray objectAtIndex:indexPath.row];
    XJCMusciModel *model1 = [[XJCMusciModel alloc] init];
    model1.id = model.id;
    model1.name = model.name;
    model1.pic_640 = model.pic_640;
    model1.channel = [NSMutableDictionary dictionaryWithObjectsAndKeys:model.channel, @"name", nil];
    
    cell.musicM = model1;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 170 * SCALEH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MusicDetailViewController *musicDVC = [MusicDetailViewController sharePlayerViewController];
    [musicDVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    MusicModel *model = [_dataArray objectAtIndex:indexPath.row];
    XJCMusciModel *model1 = [[XJCMusciModel alloc] init];
    model1.id = model.id;
    model1.name = model.name;
    model1.pic_640 = model.pic_640;
    model1.channel = [NSMutableDictionary dictionaryWithObjectsAndKeys:model.channel, @"name", nil];
    musicDVC.detailId = model1.id;
    musicDVC.musicM = model1;
    [self presentViewController:musicDVC animated:YES completion:nil];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
